﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Grishko.UIControls
{
    [ToolboxData("<{0}:Input_int runat=server></{0}:Input_int>"),
    DefaultProperty("Text")]
    public class Input_int : WebControl,IPostBackDataHandler
    {
        string ViewStateKey=string .Empty ;
        string ViewStateModeKey = string.Empty;
        public Input_int ()
        {
             
             
        }
        public bool EditMode
        {
            get
            {
                ViewStateModeKey = this.ClientID + "_ModeKey";
                object text = ViewState[ViewStateModeKey];
                if (null == text)
                    return false ;
                else
                    return Convert.ToBoolean (text);
            }
            set
            {
                ViewStateModeKey = this.ClientID + "_ModeKey";
                ViewState[ViewStateModeKey] = value;
            }
        }
        
        public string TextValue 
        {

            get
            {
                ViewStateKey = this.ClientID + "_TextValue";
                object text = ViewState[ViewStateKey];
                if (null == text)
                    return string.Empty;
                else
                    return (string)text;
            }
            set
            {
                ViewStateKey = this.ClientID + "_TextValue";
                ViewState[ViewStateKey] = value;
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            Page.VerifyRenderingInServerForm(this);
            base.Render(writer);

            Table tbl = new Table() { Width = new Unit("100%"), CellPadding = 0, CellSpacing = 0 };
            TableRow rw = new TableRow();
            TableCell Cell1 = new TableCell();
            TableCell Cell2 = new TableCell() { Width = new Unit("20px") };

            if (EditMode)
            {
                TextBox text = new TextBox() { Width = new Unit("99%") }; ;
                text.Text = TextValue;
                text.ID = this.UniqueID;
                Cell1.Controls.Add(text);

                ImageButton btn = new ImageButton() { Width = new Unit("99%") };
                Cell2.Controls.Add(btn);

                rw.Cells.Add(Cell1);
                rw.Cells.Add(Cell2);
            }
            else
            {
                Cell1.Text = TextValue;
                HiddenField hf = new HiddenField();
                hf.Value = TextValue;
                hf.ID = this.UniqueID;
                Cell2.Controls.Add(hf);

                ImageButton btn = new ImageButton() { Width = new Unit("99%") };
                Cell2.Controls.Add(btn);

                rw.Cells.Add(Cell1);
                rw.Cells.Add(Cell2);
            }

            tbl.Rows.Add(rw);
            tbl.RenderControl(writer);
        }

        #region IPostBackDataHandler Members


        public bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
        {
            if (postDataKey == this.UniqueID)
            {
                return true;
            }
            return false;
        }

        public void RaisePostDataChangedEvent()
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
